package com.mrobuy.jd.apply;

import com.gavel.database.SQLExecutor;
import com.gavel.utils.StringUtils;
import com.jd.open.api.sdk.JdClient;
import com.mrobuy.jd.JdClientFactory;
import com.mrobuy.jd.api.Jos;
import com.mrobuy.jdvc.entity.VCAuditInfo;
import com.mrobuy.jdvc.entity.VCAuditRecord;
import com.mrobuy.jdvc.entity.VCProductApply;
import com.mrobuy.util.IdentifierGen;

import java.util.List;

public class VcItemProductApplyGet {

    public static void main(String[] args) throws Exception {

        SQLExecutor.execute("update VC_AUDIT_INFO set ZT = 0");

        List<VCProductApply> productApplies = SQLExecutor.executeQueryBeanList("select * from VC_PRODUCT_APPLY where ZT = 1 and REJECT = ? ", VCProductApply.class, 1);
        for (VCProductApply productApply : productApplies) {
            loadAuditInfo(productApply.getApplyId(), JdClientFactory.jdClient());
        }
    }

    public static void loadAuditInfo(String applyId, JdClient client) throws Exception {


        VCAuditInfo auditInfo = Jos.productApplyAuditGetClient(client)
                                    .setApplyId(applyId)
                                    .call();

        if ( auditInfo!=null && StringUtils.isNotBlank(auditInfo.getApplyId())) {

            VCAuditInfo exist = SQLExecutor.executeQueryBean("select * from VC_AUDIT_INFO where APPLYID = ? ", VCAuditInfo.class, applyId);
            if ( exist==null ) {
                SQLExecutor.insert(auditInfo);
            } else {
                SQLExecutor.update(auditInfo);
            }


            for (VCAuditRecord auditRecord : auditInfo.getAuditRecords()) {

                VCAuditRecord existRecord = SQLExecutor.executeQueryBean("select * from VC_AUDIT_RECORD where APPLYID = ? and APPROVETIME = ? ", VCAuditRecord.class, applyId, auditRecord.getApprovetime());

                if ( existRecord==null ) {
                    auditRecord.setId(IdentifierGen.uuid());
                    SQLExecutor.insert(auditRecord);
                } else {
                    auditRecord.setId(existRecord.getId());
                    SQLExecutor.update(auditRecord);
                }
            }
        }

    }

}
